package org.neframework.webtest.service;

import java.util.List;
import java.util.Map;

import org.neframework.webtest.model.t_fk_auth;
import org.neframework.webtest.util.tree.TreeTools;

import com.tmsps.neframework.core.utils.StringTools;
import com.tmsps.neframework.jpa.base.BaseService;

public class AuthService extends BaseService {

	public static void main(String[] args) {
		AuthService s = new AuthService();
		List<Map<String, Object>> list = s.selectAuthList();
		System.err.println(TreeTools.turnListToTree(list));
	}

	public List<Map<String, Object>> selectAuthList() {
		// TODO 查询所有权限数据
		String sql = "select * from t_fk_auth t where t.status=0 order by t.code asc";
		List<Map<String, Object>> list = jc.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> selectSystemAuthList(String system) {
		// TODO 查询 系统的 所有菜单和操作项
		String sql = "select * from t_fk_auth t where t.status=0 and t.system=? order by t.code asc";
		List<Map<String, Object>> list = jc.queryForList(sql, new String[] { system });
		return list;
	}

	public List<Map<String, Object>> selectMenuAuthList(String system) {
		// TODO 查询所有菜单
		String sql = "select * from t_fk_auth t where t.status=0 and t.type='菜单' and t.system=? order by t.code asc";

		List<Map<String, Object>> list = jc.queryForList(sql, new String[] { system });
		return list;
	}

	public List<Map<String, Object>> selectMenuAuthList(String system, String role_codes) {
		// TODO 查询 用户菜单
		String codes = StringTools.splitStrToSqlIn(role_codes);

		String sql = "select * from t_fk_auth t where t.status=0 and t.code in (@codes) order by t.code asc";
		sql = sql.replace("@codes", codes);

		List<Map<String, Object>> list = jc.queryForList(sql);
		return list;
	}

	public t_fk_auth findAuthByCode(String code) {
		// TODO 根据code查询是否存在
		String sql = "select * from t_fk_auth t where t.status=0 and t.code=?";
		t_fk_auth auth = (t_fk_auth) oc.findObj(sql, new String[] { code }, t_fk_auth.class);
		return auth;
	}

	public t_fk_auth findAuthByCode(String kid, String code) {
		String sql = "select * from t_fk_auth t where t.status=0 and t.code=? and t.kid!=?";
		t_fk_auth auth = (t_fk_auth) oc.findObj(sql, new String[] { code, kid }, t_fk_auth.class);
		return auth;
	}

}
